<?php
class Profile{
    var $use_id;
    var $use_login;
    var $use_password;
    var $use_first_name;
    var $use_last_name;
    var $use_birthdays;
    var $use_showbd_full;
    var $use_showbd_year;
    var $use_gender;
    var $use_city;
    var $use_phone;
    var $use_status;
    var $use_job;
    var $use_fax;
    var $use_email;
    var $use_email2;
    var $use_email2_active;
    var $use_address;
    var $use_date;
    var $use_group;
    var $use_name;
    var $use_admin;
    var $use_avatar;
    var $use_openid;
    var $use_facebook_id;
    var $use_showphone;
    var $use_online;
    var $use_showonline;
    var $use_receive_msg;
    var $use_receive_mail_noti;
    var $fs_path_avatar = '../pictures/avatar/';
    /** Kết thúc phần thông tin user **/
    
    
    public function __construct($user_id=0){
        if(!$user_id){
            $users = new user();
            $this->use_id = $users->u_id;    
        }
        else $this->use_id = $user_id;
        $db_sl = new db_query('SELECT * FROM users WHERE use_id = '.$this->use_id);
        
        if(mysql_num_rows($db_sl->result)){
            $tmpuser = mysql_fetch_assoc($db_sl->result);
            $this->use_address = ($tmpuser['use_address']) ? $tmpuser['use_address'] : '';
            $this->use_admin = ($tmpuser['use_admin']) ? $tmpuser['use_admin'] : '';
            $this->use_avatar = ($tmpuser['use_avatar'] && file_exists($this->fs_path_avatar.$tmpuser['use_avatar'])) ? $tmpuser['use_avatar'] : 'noavatar.jpg';
            $this->use_birthdays = ($tmpuser['use_birthdays']) ? $tmpuser['use_birthdays'] : false;
            $this->use_showbd_full = $tmpuser['use_showbd_full'];
            $this->use_showbd_year = $tmpuser['use_showbd_year'];
            $this->use_city = ($tmpuser['use_city']) ? $tmpuser['use_city'] : '';
            $this->use_date = $tmpuser['use_date'];
            $this->use_email = ($tmpuser['use_email']) ? $tmpuser['use_email'] : '';
            $this->use_email2 = ($tmpuser['use_email2']) ? $tmpuser['use_email2'] : '';
            $this->use_email2_active = ($tmpuser['use_email2_active']) ? $tmpuser['use_email2_active'] : 0;
            $this->use_facebook_id = $tmpuser['use_facebook_id'];
            $this->use_fax = ($tmpuser['use_fax']) ? $tmpuser['use_fax'] : '';
            $this->use_first_name = ($tmpuser['use_first_name']) ? $tmpuser['use_first_name'] : '';
            $this->use_job = ($tmpuser['use_job']) ? $tmpuser['use_job'] : '';
            $this->use_status = ($tmpuser['use_status']) ? $tmpuser['use_status'] : '';
            $this->use_gender = $tmpuser['use_gender'];
            $this->use_group = $tmpuser['use_group'];
            $this->use_last_name = $tmpuser['use_last_name'];
            $this->use_login = $tmpuser['use_login'];
            $this->use_name = $tmpuser['use_name'];
            $this->use_openid = $tmpuser['use_openid'];
            $this->use_password = $tmpuser['use_password'];
            $this->use_phone = ($tmpuser['use_phone']) ? $tmpuser['use_phone'] : '';
            $this->use_showphone = ($tmpuser['use_showphone']) ? $tmpuser['use_showphone'] : 0;
            $this->use_online = ($tmpuser['use_online']) ? $tmpuser['use_online'] : 0;
            $this->use_showonline = ($tmpuser['use_showonline']) ? $tmpuser['use_showonline'] : 0;
            $this->use_receive_msg = ($tmpuser['use_receive_msg']) ? $tmpuser['use_receive_msg'] : 0;
            $this->use_receive_mail_noti = ($tmpuser['use_receive_mail_noti']) ? $tmpuser['use_receive_mail_noti'] : 0;
        }
        unset($db_sl,$tmpuser);
    }
    public function getInfoUser($user_id = 0){
        if(!$user_id){
            return $this;
        }
        else{
            $other = new Profile($user_id);
            return $other;
        }
    }
    public function editInfoUser($use_name='',$use_job='',$use_phone='',$use_gender=0){
        
        //Check đăng nhập
        $user = new user();
        if(!$user->logged()){
            //Getout
            return false;
        }
        if($this->use_id){
            $myform = new generate_form();
            $myform->add('use_name','use_name',0,1,'',1,'Tên không được bỏ trống');
            $myform->add('use_job','use_job',0,1,'',0,'');
            $myform->add('use_phone','use_phone',0,1,'');
            $myform->add('use_job','use_job',1,1,'',0,'');
            $myform->addTable('users');
            $error = '';
            $error .= $myform->checkdata();
            if($error == ''){
                $sql_update = $myform->generate_update_SQL('use_id',$this->use_id);
                echo $sql_update;
            }
            else{
                return $error;
            }
        }
    }
    public function getAge(){
        if($this->use_birthdays){
            $now = getdate(time());
            $year = $now['year'];
            $user = getdate($this->use_birthdays);
            $born = $user['year'];
            return $year - $born;
        }
        else{
            return false;
        }
    }
    //Tính tổng số bài viết đã viết
    public function countTotalArticle(){
        //$db_count = new db_count('SELECT COUNT(*) AS count FROM user_reviews WHERE use_id = '.$this->use_id);
//        $uRev = $db_count->total;
        unset($db_count);
        $db_count = new db_count('SELECT COUNT(*) AS count FROM reviews WHERE rev_active = 1 AND rev_user_id = '.$this->use_id);
        $Rev = $db_count->total;
        unset($db_count);
        $db_count = new db_count('SELECT COUNT(*) AS count FROM news WHERE new_active = 1 AND new_user_id = '.$this->use_id);
        $News = $db_count->total;
        unset($db_count);
        return  $Rev + $News;
    }
    
    
    public function getCity(){
        if($this->use_city){
            $db_sl = new db_query('SELECT * FROM cities WHERE cit_id = '.$this->use_city);
            $city = mysql_fetch_assoc($db_sl->result);unset($db_sl);
            return $city['cit_name'].', '.$city['cit_country'];
        }
        else return '';
    }
    
    //Lấy ra danh sách các article đang theo dõi - trả về mảng ID
    public function getListArticleFollow(){
        $db = new db_query('SELECT usa_article_id FROM user_article WHERE usa_user_id = '.$this->use_id);
        $arr_return = $db->resultArray();
        unset($db);
        return $arr_return;
    }
    
    //Lấy ra danh sách các user đang follow mình
    public function getListFollowMe(){
        $db = new db_query('SELECT usf_user_id FROM user_follow WHERE usf_follow_id = '.$this->use_id);
        $array_follow_me = array();
        while($row = mysql_fetch_assoc($db->result)){
            $array_follow_me[] = $row['usf_user_id'];
        }
        unset($db);
        return $array_follow_me;
    }
    
    //Lấy ra danh sách các user mình đang follow
    public function getListPersonFollowed(){
        $db = new db_query('SELECT usf_follow_id FROM user_follow WHERE usf_user_id = '.$this->use_id);
        $array_person_followed = array();
        while($row = mysql_fetch_assoc($db->result)){
            $array_person_followed[] = $row['usf_follow_id'];
        }
        unset($db);
        return $array_person_followed;
    }
    
    //Follow 1 user
    public function FollowPerson($follow_id){
        //Nếu follow_id != use_id thì mới cho follow, insert vào bảng user_follow
        if($follow_id != $this->use_id){
            //Kiểm tra xem đã follow chưa, nếu chưa follow thì mới insert, nếu follow rồi thì trả về true luôn
            $check_follow = $this->checkFollow($follow_id);
            if($check_follow) return true;
            else{
                $db_follow = new db_execute('INSERT INTO user_follow 
                                            VALUES(
                                                '.$this->use_id.',
                                                '.$follow_id.'
                                            )');
                unset($db_follow);
                return true;
            }
        }
    }
    
    //Bỏ follow 1 user
    public function UnFollowPerson($follow_id){
        if($follow_id != $this->use_id){
            //Kiểm tra xem đã follow chưa, nếu chưa follow thì trả về true, nếu follow rồi thì delete
            $check_follow = $this->checkFollow($follow_id);
            if(!$check_follow) return true;
            else{
                $db_delete = new db_execute('DELETE FROM user_follow 
                                             WHERE usf_user_id ='.$this->use_id.' AND usf_follow_id = '.$follow_id);
                unset($db_delete);
                return true;
            }
        }
    }
    
    //Kiểm tra xem đã follow user này chưa
    public function checkFollow($follow_id){
        if($follow_id != $this->use_id){
            $db_check = new db_query('SELECT * 
                                      FROM user_follow 
                                      WHERE usf_user_id = '.$this->use_id.' AND usf_follow_id = '.$follow_id);
            if(mysql_num_rows($db_check->result) > 0){
                return true;
            }else return false;
            unset($db_check);
        }
    }
    
}
?>